package top.cdmrx.demo.edu.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import top.cdmrx.demo.edu.entity.Course;
import top.cdmrx.demo.edu.entity.CourseDescription;
import top.cdmrx.demo.edu.entity.vo.CourseInfoForm;
import top.cdmrx.demo.edu.entity.vo.CoursePublishVo;
import top.cdmrx.demo.edu.mapper.CourseMapper;
import top.cdmrx.demo.edu.service.ChapterService;
import top.cdmrx.demo.edu.service.CourseDescriptionService;
import top.cdmrx.demo.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.cdmrx.demo.edu.service.VideoService;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {

        //向课程表添加课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);

        int insert = baseMapper.insert(course);
        if (insert==0){
            throw  new RuntimeException("添加课程信息失败！");
        }

        //获取课程号的id值
        String id = course.getId();

        //向课程简介表添加基本课程简介信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        //保持与课程id一致，表示一对一的关系
        courseDescription.setId(id);
        courseDescriptionService.save(courseDescription);

        return id;
    }

    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //根据id查询课程表
        Course course = baseMapper.selectById(courseId);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(course,courseInfoForm);
        //查询描述表
        CourseDescription byId = courseDescriptionService.getById(courseId);
        courseInfoForm.setDescription(byId.getDescription());

        return courseInfoForm;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {

        //修改课程信息
        Course course=new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        int i = baseMapper.updateById(course);
        if (i<=0){
            throw  new RuntimeException("修改失败！！！");
        }

        //修改简介信息
        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setId(courseInfoForm.getId());
        courseDescription.setDescription(courseInfoForm.getDescription());

        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishVo = baseMapper.selectCoursePublishVoById(id);
        return publishVo;
    }

    @Override
    public void removeCourse(String id) {
        //先删除小节
        videoService.removeId(id);
        //再删除章节
        chapterService.removeId(id);
        //再删除描述
        courseDescriptionService.removeId(id);
        //最后删除课程本身
        int i = baseMapper.deleteById(id);
        if (i==0){
            throw  new RuntimeException("删除课程失败");
        }
    }
}
